package wahadlo.nowe.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.SpringLayout;

import wahadlo.nowe.model.ModelRecipient;
import wahadlo.nowe.model.ModelSupplier;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private AxisComponent axisComponent;
	private AuthorComponent authorComponent;
	private TypeComponent typeComponent;
	private SmallVibComponent smallVibComponent;
	private DataPanel dataPanel;
	private ControllPanel controllPanel;
	private Dimension size = new Dimension(900, 475);

	private void init(Container panel) {
		setMinimumSize(size);
		setMaximumSize(size);
		setTitle("Wahadło Matematyczne");
		SpringLayout layout = new SpringLayout();
		panel.setLayout(layout);

		axisComponent = new AxisComponent();
		authorComponent = new AuthorComponent();
		typeComponent = new TypeComponent();
		smallVibComponent = new SmallVibComponent();
		dataPanel = new DataPanel();

		List<ModelSupplier> modelSuppliers = new Vector<ModelSupplier>(3);
		modelSuppliers.add(typeComponent);
		modelSuppliers.add(smallVibComponent);
		modelSuppliers.add(dataPanel);

		List<ModelRecipient> modelRecipients = new Vector<ModelRecipient>();
		modelRecipients.add(axisComponent);
		
		controllPanel = new ControllPanel(modelSuppliers, modelRecipients);

		addComponents(panel);

		prepareLayout(layout);

		pack();

	}

	private void addComponents(Container panel) {
		panel.add(axisComponent);
		panel.add(authorComponent);
		panel.add(typeComponent);
		panel.add(smallVibComponent);
		panel.add(controllPanel);
		panel.add(dataPanel);
	}

	private void prepareLayout(SpringLayout layout) {
		layout.putConstraint(SpringLayout.NORTH, axisComponent, 0,
				SpringLayout.NORTH, getContentPane());
		layout.putConstraint(SpringLayout.WEST, axisComponent, 0,
				SpringLayout.WEST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, authorComponent, 0,
				SpringLayout.SOUTH, getContentPane());
		layout.putConstraint(SpringLayout.EAST, authorComponent, 0,
				SpringLayout.EAST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, typeComponent, -5,
				SpringLayout.SOUTH, getContentPane());
		layout.putConstraint(SpringLayout.WEST, typeComponent, 0,
				SpringLayout.WEST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, smallVibComponent, -10,
				SpringLayout.NORTH, authorComponent);
		layout.putConstraint(SpringLayout.EAST, smallVibComponent, -10,
				SpringLayout.EAST, authorComponent);

		layout.putConstraint(SpringLayout.NORTH, controllPanel, 5,
				SpringLayout.NORTH, getContentPane());
		layout.putConstraint(SpringLayout.EAST, controllPanel, -5,
				SpringLayout.EAST, getContentPane());

		layout.putConstraint(SpringLayout.NORTH, dataPanel, 5,
				SpringLayout.SOUTH, controllPanel);
		layout.putConstraint(SpringLayout.EAST, dataPanel, -5,
				SpringLayout.EAST, getContentPane());
	}

	public MainFrame() {
		init(this.getContentPane());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
